# 数据结构和算法

# Q: 一个包含N个元素的元祖或序列,分解为N个单独的变量
def answerQ1():
	o,p,q = ("嘿嘿嘿",'a',123)
	return "o : "+o+" p ： "+p+" q : "+str(q)

# Q: 从某个可迭代对象中分解出N个元素, 如果可迭代对象长度超过N,会导致出现"too many values to upack"的异常
def drop_first_last(grades):
	first,*middle,last = grades
	return middle

from collections import deque

# Q: 希望在迭代或者其他形式的处理过程中对最后几项纪录做个有限的历史记录统计
def search(lines,pattern,history=5):
	previous_lines =deque(maxlen=history)
	for line in lines:
		if pattern in line:
			yield line,previous_lines
		previous_lines.append(line)

if __name__ == '__main__':
	print("answerQ1: ",answerQ1())
	print("answerQ2: ",str(drop_first_last((1,2,3,4,5))))
	# answerQ3
	print("answerQ3---------------------")
	with open('demo.txt') as f:
		for line,prevlines in search(f,'Python',2):
			for pline in prevlines:
				print(pline,end='')
			print(line,end='')
			print('-'*20)